//
//  ViewController.m
//  11-26tableView
//
//  Created by blue on 15-11-26.
//  Copyright (c) 2015年 hyd. All rights reserved.
//

#import "ViewController.h"
#import "inputViewController.h"
@interface ViewController ()<EditProtocol>
{

    NSMutableArray * names;
    NSMutableArray * tels;
    NSIndexPath * curIndexPath;
}
@end

@implementation ViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    self.title = @"我的通讯录";
   names = @[
             @"明明",
             @"伟伟",
             @"菲菲",
             ].mutableCopy;
    
    tels = @[
             @"18245875569",
             @"14785962135",
             @"13652141789"
             ].mutableCopy;
	self.tableView = [[UITableView alloc]initWithFrame:self.view.bounds style:UITableViewStylePlain];
    self.tableView.dataSource = self;
    self.tableView.delegate = self;
    [ self.view addSubview:self.tableView];
//    NSUserDefaults * def = [NSUserDefaults standardUserDefaults];
//    [def setObject:data forKey:@"data"];
//    [def setObject:tels forKey:@"tels"];
//    [def synchronize];
    
    // Do any additional setup after loading the view, typically from a nib.
//    data = [[def objectForKey:@"data"] mutableCopy];
//    tels = [[def objectForKey:@"tels"] mutableCopy];//存储和写入硬盘
    
//    self.navigationItem.rightBarButtonItem = self.editButtonItem;
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"编辑" style:UIBarButtonItemStylePlain target:self action:@selector(onEditing:)];

}


-(void)onEditing:(UIBarButtonItem *)sender
{
    [self.tableView setEditing:!self.editing animated:YES];
    sender.title = self.editing ? @"编辑":@"完成";
    [self.tableView reloadData];//刷新
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{

    return tableView.isEditing ? names.count+1:names.count;

}

-(UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return indexPath.row - names.count ? UITableViewCellEditingStyleDelete:UITableViewCellEditingStyleInsert;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{

    UITableViewCell * cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:nil];
    
    if (indexPath.row < names.count)
    {
        cell.textLabel.text = names[indexPath.row];
        cell.detailTextLabel.text = tels[indexPath.row];
    }
    
    
    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    return cell;

}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{

//    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    inputViewController * con = [[inputViewController alloc]initWithStyle:UITableViewStyleGrouped];
    if (indexPath.row < names.count) {
        con.name = names[indexPath.row];
        con.tel = tels[indexPath.row];
    }else
    {
        [names addObject:@""];
        [tels addObject:@""];
    }
    curIndexPath = indexPath;
    con.deledate = self;
    [self.navigationController pushViewController:con animated:YES];

    
}


-(void)editssName:(NSString *)name tel:(NSString *)tel
{

    NSMutableArray * arr = names.mutableCopy;
    [arr replaceObjectAtIndex:curIndexPath.row withObject:tel];
    names = arr;
    NSUserDefaults * def = [NSUserDefaults standardUserDefaults];
    [def setObject:names forKey:@"names"];
    
    NSIndexPath * indexPath = self.tableView.indexPathForSelectedRow;
    names[indexPath.row] = name;
    tels[indexPath.row] = tel;
    
    [self.tableView reloadData];

}

-(void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
    [names removeObjectAtIndex:indexPath.row];
    [tels removeObjectAtIndex:indexPath.row];
    [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationTop];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
